/**
 * 生成掉落的方块
 */
import { rotate } from './matrix'
export class Box {
  constructor () {
    this.x = 0
    this.y = 0
    this.shape = [
      [1, 1],
      [1, 1]
    ]
  }

  _rotates = []
  _rotateIndex = 0
  rotate () {
    const rotateFn = this._rotates[this._rotateIndex]
    if (!rotateFn) return
    this.shape = rotateFn(this.shape)
    this._rotateIndex++
    if (this._rotateIndex >= this._rotates.length) {
      this._rotateIndex = 0
    }
  }

  setStrategy (v) {
    if (!v) return
    this._rotates = v
  }
};
// 1. oop 继承 不同box，不同shape
// 2. 组合 修改生成的box
// 下面为组合

const boxInfos = {
  1: {
    shape: [
      [1, 1],
      [1, 1]
    ]
  },
  2: {
    shape: [
      [1, 0, 0],
      [1, 1, 0],
      [0, 1, 0]
    ],
    rotateStrategy: [rotate, (m) => rotate(rotate(rotate(m)))]// 策略模式
  },
  3: {
    shape: [
      [1, 0, 0],
      [1, 0, 0],
      [1, 1, 0]
    ],
    rotateStrategy: [rotate, rotate, rotate, rotate]
  }
}
// 简单工厂模式
// vue3 -> createApp createStore
export function createBox () {
  const box = new Box()

  const { shape, rotateStrategy } = getRandomBoxInfo()

  box.shape = shape
  box.setStrategy(rotateStrategy)
  return box
}
function getRandomBoxInfo () {
  const max = Object.keys(boxInfos).length
  const key = Math.floor(Math.random() * max) + 1// 随机生成方块
  //   const key = 1
  return boxInfos[key]
}
